package com.yisu.auction.api.web.controller;

import com.alibaba.fastjson.JSON;
import com.yisu.auction.api.core.service.IMemberService;
import com.yisu.auction.api.core.service.IRechargeService;
import com.yisu.auction.api.core.service.ISystemConfigService;
import com.yisu.auction.api.domain.request.rechargePackage.RechargeRequest;
import com.yisu.auction.api.domain.response.activityPackage.NewUserRechargeResponse;
import com.yisu.auction.api.domain.vo.activityPackageVos.NewUserRechargeVo;
import com.yisu.auction.api.domain.vo.indexPackageVos.SystemConfigVo;
import com.yisu.auction.api.web.interceptor.IAuthVerifyAnnotation;
import com.yisu.auction.api.web.utils.PingppUtil;
import com.yisu.auction.ewf.common.enums.LoginTypeEnum;
import com.yisu.auction.ewf.common.enums.ResponseCode;
import com.yisu.auction.ewf.common.web.JsonResult;
import org.apache.calcite.linq4j.Linq4j;
import org.apache.calcite.linq4j.function.Predicate1;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * author: 赵杰
 * Business Description： 充值业务
 * createTime：2017/8/19
 */
@Controller
@RequestMapping(value = "/recharge")
public class RechargeController extends BaseController {
    @Resource
    private IRechargeService iRechargeService;
    @Resource
    private ISystemConfigService iSystemConfigService;
    @Resource
    private IMemberService iMemberService;

    /**
     * author: 赵杰
     * Business Description：获取首页相关业务信息
     *
     * @param
     * @return 系统参数
     */
    @RequestMapping(value = "/createRechargeOrder", method = {RequestMethod.POST,RequestMethod.GET}, produces = "application/json; charset=utf-8")
    @ResponseBody
    @IAuthVerifyAnnotation(Desc = "登录验证", LoginTypeEnum = LoginTypeEnum.NEED_LOGIN)
    public JsonResult createRechargeOrder(RechargeRequest rechargeRequest) throws InterruptedException, UnsupportedEncodingException {
        if (rechargeRequest.getAmount() == null ||  (rechargeRequest.getPayType() == null)) {
            return new JsonResult(ResponseCode.PARAMETER_ERROR.code, ResponseCode.PARAMETER_ERROR.msg);
        }
        this.userToken = rechargeRequest.getUserToken();
        rechargeRequest.setMemberId(String.valueOf(this.getToken().getMemberId()));
        String orderNo = iRechargeService.createRechargeOrder(rechargeRequest);
        String chargeStr= PingppUtil.getChargeCommon(orderNo,rechargeRequest.getAmount()+"",rechargeRequest.getPayType(),rechargeRequest.getCallbackUrl(),rechargeRequest.getAppSign());
        if (!StringUtils.isEmpty(chargeStr)) {
            JsonResult result = new JsonResult();
            result.setData(chargeStr);
            return result;
        }
        return new JsonResult(ResponseCode.FAILURE.code, ResponseCode.FAILURE.msg);
    }


    /**
     * author: 赵杰
     * Business Description：新用户首冲活动
     *
     * @param
     * @return 系统参数
     */

    @RequestMapping(value = "/newUserRechargeActivity", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    @ResponseBody
    @IAuthVerifyAnnotation(Desc = "登录验证", LoginTypeEnum = LoginTypeEnum.NO_NEED_LOGIN)
    public JsonResult newUserRechargeActivity(RechargeRequest rechargeRequest) {
        this.userToken = rechargeRequest.getUserToken();
        rechargeRequest.setMemberId(String.valueOf(this.getToken().getMemberId()));
        NewUserRechargeResponse newUserRechargeResponse = new NewUserRechargeResponse();
        JsonResult jsonResult = new JsonResult();
        String banner ="http://qiniu.ddbaoxiang.com/170915104816578635983879.jpg";
        // TODO: 2017/9/15  新用户暂不设定，吸引用户前来充值
        int isNewUser =1;// iMemberService.returnIsNewUser(rechargeRequest);
        SystemConfigVo configValue = iSystemConfigService.getSystemConfigByKey("newUserRecharge");
        List<NewUserRechargeVo> newUserRechargeVos = JSON.parseArray(configValue.getConfigValue(), NewUserRechargeVo.class);
        for (NewUserRechargeVo item:newUserRechargeVos) {
            item.setGivePresent(String.valueOf("送" + item.getGivePresent()));
        }
        newUserRechargeResponse.setBanner(banner);
        newUserRechargeResponse.setNewUserRechargeVos(newUserRechargeVos);
        newUserRechargeResponse.setIsNewUser(isNewUser);
        jsonResult.setData(newUserRechargeResponse);
        return jsonResult;
    }


    /**
     * author: 赵杰
     * Business Description：新用户首冲活动
     *
     * @param
     * @return 系统参数
     */

    @RequestMapping(value = "/newUserRecharge", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    @ResponseBody
    @IAuthVerifyAnnotation(Desc = "登录验证", LoginTypeEnum = LoginTypeEnum.NEED_LOGIN)
    public JsonResult newUserRecharge(final RechargeRequest rechargeRequest) throws InterruptedException, UnsupportedEncodingException {
        if (StringUtils.isEmpty(rechargeRequest.getAmount()) || rechargeRequest.getPayType() == null) {
            return new JsonResult(ResponseCode.FAILURE.code, "参数不完整");
        }
        this.userToken = rechargeRequest.getUserToken();
        rechargeRequest.setMemberId(String.valueOf(this.getToken().getMemberId()));
        SystemConfigVo configValue = iSystemConfigService.getSystemConfigByKey("newUserRecharge");
        List<NewUserRechargeVo> newUserRechargeVos = JSON.parseArray(configValue.getConfigValue(), NewUserRechargeVo.class);
        if (CollectionUtils.isEmpty(newUserRechargeVos)) {
            return new JsonResult(-1, "非法请求");
        }
        NewUserRechargeVo newUserRechargeVo = Linq4j.asEnumerable(newUserRechargeVos).where(new Predicate1<NewUserRechargeVo>() {
            @Override
            public boolean apply(NewUserRechargeVo item) {
                return (item.getRechargeAmount()+"").equals(rechargeRequest.getAmount());
            }
        }).firstOrDefault();
        if (newUserRechargeVo == null) {
            return new JsonResult(-1, "非法请求");
        }
        rechargeRequest.setPayType(rechargeRequest.getPayType());
        rechargeRequest.setAmount(rechargeRequest.getAmount());
        rechargeRequest.setDescription(JSON.toJSONString(newUserRechargeVo));
        rechargeRequest.setOrderSign("NEWUSERRECHARGE");
        rechargeRequest.setMemberId(rechargeRequest.getMemberId());
        rechargeRequest.setAppType(rechargeRequest.getAppType());
        String orderNo = iRechargeService.createRechargeOrder(rechargeRequest);
        String chargeStr = PingppUtil.getChargeCommon(orderNo, rechargeRequest.getAmount() + "", rechargeRequest.getPayType(),"",rechargeRequest.getAppSign());
        if (!StringUtils.isEmpty(chargeStr)) {
            JsonResult result = new JsonResult();
            result.setData(chargeStr);
            return result;
        }
        return new JsonResult(ResponseCode.FAILURE.code, ResponseCode.FAILURE.msg);
    }

}
